#include <stdio.h>

#define SQ_LIST
// #define LINK_LIST
// #define LOOP_LINK_LIST

#if defined(SQ_LIST)            // 顺序表
#include "sqlist.h"
#include "list.h"

void setListOper()
{
    initList        = initSqList;
    destroyList     = destroySqList;
    clearList       = clearSqList;
    listEmpty       = sqListEmpty;
    listLength      = sqListLength;
    getElem         = getSqListElem;
    locateElem      = locateSqListElem;
    priorElem       = priorSqListElem;
    nextElem        = nextSqListElem;
    listInsert      = insertSqList;
    listDelete      = deleteSqList;
    traverseList    = traverseSqList;
}
#elif defined(LINK_LIST)        // 单链表
#include "linklist.h"
#include "list.h"

void setListOper()
{
    initList        = initLinkList;
    destroyList     = destroyLinkList;
    clearList       = clearLinkList;
    listEmpty       = linklistIsEmpty;
    listLength      = linklistLength;
    getElem         = getLinkListElem;
    locateElem      = locateLinkListElem;
    priorElem       = priorLinkListElem;
    nextElem        = nextLinkListElem;
    listInsert      = linklistInsert;
    listDelete      = linklistDelete;
    traverseList    = traverseLinkList;
}
#elif defined(LOOP_LINK_LIST)       // 单向循环链表
#include "looplink.h"
#include "list.h"

void setListOper()
{
    initList        = initLoopLinkList;
    destroyList     = destroyLoopLinkList;
    clearList       = clearLoopLinkList;
    listEmpty       = loopLinkListIsEmpty;
    listLength      = loopLinkListLength;
    getElem         = getLoopLinkListElem;
    locateElem      = locateLoopLinkListElem;
    priorElem       = priorLoopLinkListElem;
    nextElem        = nexrLoopLinkListElem;
    listInsert      = loopLinkListInsert;
    listDelete      = loopLinkListDelete;
    traverseList    = traverseLoopLinkList;
}

#endif

int main()
{
    List L;

    setListOper();

    // 初始化
    initList(&L);

    if (listEmpty(&L) == 1)
        printf("空表\n");
    else
        printf("非空表");

    // 插入10个元素
    int i;
    for (i = 0; i < 10; i++)
        listInsert(&L, i, (i + 1) * (i + 1));

    printf("成功插入10个元素\n");

    // 遍历线性表
    traverseList(&L);

    // 在第5个位置插入新元素 21
    if (listInsert(&L, 5, 21) == 1)
        printf("成功插入新元素:%d\n", 21);
    else
        printf("插入新元素失败\n");

    // 求线性表的长度
    int len = listLength(&L);
    printf("线性表的长度为:%d\n", len);

    // 遍历线性表
    traverseList(&L);

    // 删除第3个元素
    ElemType e;
    if (listDelete(&L, 10, &e) == 1)
        printf("成功删除下标为%d的元素:%d\n", 3, e);
    else
        printf("删除下标为%d的元素失败\n", 3);

    // 遍历线性表
    len = listLength(&L);
    printf("线性表的长度为:%d\n", len);
    traverseList(&L);

    // 销毁线性表
    destroyList(&L);

    // 返回
    return 0;
}

